import { reqspecscount, reqspecslist } from "../../http/api"

let state = {
    arr: [],
    total: 0,
    size: 2,
    page: 1
}
let getters = {
    arr({ arr }) {
        return arr
    },
    total({ total }) {
        return total
    },
    size({ size }) {
        return size
    },
}
let mutations = {
    changeArr(state, arr) {
        state.arr = arr
    },
    changeTotal(state, total) {
        state.total = total
    },
    changePage(state, page) {
        state.page = page
    },
}
let actions = {
    reqList({ state: { page, size }, commit, dispatch }, all = false) {
        let params = all ? {} : { page, size }
        reqspecslist(params).then(res => {
            if (res.data.code === 200) {
                let arr = res.data.list ? res.data.list : []
                if (arr.length == 0 && page != 1) {
                    commit("changePage", page - 1)
                    dispatch("reqList")
                    return
                }
                commit("changeArr", arr)
            }
        })
    },
    reqArr({ commit, dispatch }) {
        dispatch("reqList")
        reqspecscount().then(res => {
            if (res.data.code === 200) {
                commit("changeTotal", res.data.list[0].total)
            }
        })
    },
    changePage({ commit, dispatch }, e) {
        commit("changePage", e)
        dispatch("reqList")
    }
}

export default {
    state,
    getters,
    mutations,
    actions,
    namespaced: true
}